/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.institution.approval;

import com.alibaba.fastjson.JSONObject;
import com.koron.audit.approval.AfterApprovalService;
import com.koron.audit.domain.Audit;
import com.koron.institution.bean.InstitutionFile;
import com.koron.institution.bean.InstitutionFileHistory;
import com.koron.institution.mapper.InstitutionFileHistoryMapper;
import com.koron.institution.mapper.InstitutionFileMapper;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class InstitutionAddService implements AfterApprovalService {

    private final static  Logger logger = LoggerFactory.getLogger(InstitutionAddService.class);

    @Override
    public void editApproval(Audit audit) {
        SessionFactory factory = new SessionFactory();
        try {
            List<String> idList = JSONObject.parseArray(audit.getBusinessIds(), String.class);
            if (idList != null && !idList.isEmpty()) {
                for (String planId : idList) {
                    if (StringUtils.isNotEmpty(planId)) {
                        InstitutionFileMapper mapper = factory.getMapper(InstitutionFileMapper.class);
                        InstitutionFileHistoryMapper historyMapper = factory.getMapper(InstitutionFileHistoryMapper.class);

                        InstitutionFileHistory history = historyMapper.selectByPrimaryKey(planId);
                        if (history != null) {
//                            InstitutionFile institutionFile = translateHistoryToInstitution(history);
                            List<InstitutionFile> institutionFiles = mapper.queryInstitutionByCode(history.getCode());
                            if (institutionFiles != null && !institutionFiles.isEmpty()) {
                                for (InstitutionFile institutionFile : institutionFiles) {
                                    if (institutionFile.getState() == 4) {
                                        continue;
                                    }
                                    institutionFile.setState(0);
                                    mapper.updateByPrimaryKeySelective(institutionFile);
                                }
                            }
                            history.setState(0);
                            history.setHistoryFlag(0);
                            historyMapper.updateByPrimaryKeySelective(history);
                        }
                    }
                }
            }
            factory.close();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory)
                factory.close(false);
        }
    }

    @Override
    public void discardApproval(Audit audit) {
        SessionFactory factory = new SessionFactory();
        try {
            List<String> idList = JSONObject.parseArray(audit.getBusinessIds(), String.class);
            if (idList != null && !idList.isEmpty()) {
                for (String planId : idList) {
                    if (StringUtils.isNotEmpty(planId)) {
                        InstitutionFileMapper mapper = factory.getMapper(InstitutionFileMapper.class);
                        InstitutionFileHistoryMapper historyMapper = factory.getMapper(InstitutionFileHistoryMapper.class);
//                    InstitutionFile institutionFile = mapper.selectByPrimaryKey(planId);
//                    if (institutionFile != null) {
//                        InstitutionFileHistory history = translateInstitutionToHistory(institutionFile);
//                        historyMapper.insert(history);
//                        mapper.deleteByPrimaryKey(institutionFile.getId());
//                    }
                        InstitutionFileHistory history = historyMapper.selectByPrimaryKey(planId);
                        if (history != null) {
                            List<InstitutionFile> institutionFiles = mapper.queryInstitutionByCode(history.getCode());
                            if (institutionFiles != null && !institutionFiles.isEmpty()) {
                                for (InstitutionFile institutionFile : institutionFiles) {
                                    if (institutionFile != null) {
                                        institutionFile.setState(4);
                                        mapper.updateByPrimaryKeySelective(institutionFile);
                                    }
//                                    mapper.deleteByPrimaryKey(institutionFile.getId());
                                }
                            }
                            history.setState(0);
                            history.setHistoryFlag(1);
                            historyMapper.updateByPrimaryKeySelective(history);
                        }
                    }
                }

            }
            factory.close();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory)
                factory.close(false);
        }
    }

    @Override
    public void processDiscard(Audit audit) {
        try {
            List<String> idList = JSONObject.parseArray(audit.getBusinessIds(), String.class);
            if (audit.getOperation() == 1) {
                processDiscardAdd(idList);
            } else if (audit.getOperation() == 2) {
                processDiscardDel(idList);
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }


    private void processDiscardAdd(List<String> idList) {
        SessionFactory factory = new SessionFactory();
        try {
            if (idList != null && !idList.isEmpty()) {
                for (String planId : idList) {
                    if (StringUtils.isNotEmpty(planId)) {
                        InstitutionFileMapper mapper = factory.getMapper(InstitutionFileMapper.class);
                        InstitutionFileHistoryMapper historyMapper = factory.getMapper(InstitutionFileHistoryMapper.class);
                        InstitutionFileHistory history = historyMapper.selectByPrimaryKey(planId);
                        if (history != null) {
                            List<InstitutionFile> institutionFiles = mapper.queryInstitutionByCode(history.getCode());
                            if (institutionFiles != null && !institutionFiles.isEmpty()) {
                                for (InstitutionFile institutionFile : institutionFiles) {
                                    if (institutionFile.getState() == 4) {
                                        continue;
                                    }
                                    institutionFile.setState(5);
                                    mapper.updateByPrimaryKeySelective(institutionFile);
                                }
                            }
                            history.setState(4);
                            history.setHistoryFlag(0);
                            historyMapper.updateByPrimaryKeySelective(history);
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            factory.close(false);
        } finally {
            if (null != factory)
                factory.close();
        }
    }

    private void processDiscardDel(List<String> idList) {
        SessionFactory factory = new SessionFactory();
        try {
            if (idList != null && !idList.isEmpty()) {
                for (String planId : idList) {
                    if (StringUtils.isNotEmpty(planId)) {
                        InstitutionFileMapper mapper = factory.getMapper(InstitutionFileMapper.class);
                        InstitutionFileHistoryMapper historyMapper = factory.getMapper(InstitutionFileHistoryMapper.class);

                        InstitutionFileHistory history = historyMapper.selectByPrimaryKey(planId);
                        if (history != null) {
                            List<InstitutionFile> institutionFiles = mapper.queryInstitutionByCode(history.getCode());
                            if (institutionFiles != null && !institutionFiles.isEmpty()) {
                                for (InstitutionFile institutionFile : institutionFiles) {
                                    if (institutionFile != null) {
                                        institutionFile.setState(0);
                                        mapper.updateByPrimaryKeySelective(institutionFile);
                                    }
                                }
                            }
                            history.setState(4);
                            history.setHistoryFlag(0);
                            historyMapper.updateByPrimaryKeySelective(history);
                        }
                    }
                }

            }
            factory.close();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory)
                factory.close(false);
        }
    }

    @Override
    public void reject(Audit audit, List<String> userIdList) {
        // TODO 流程驳回操作
    }

    @Override
    public void passApproval(Integer operation, Audit audit) {
        // TODO 流程提交成功操作
    }
}
